(define uvfacets_view "")
(define uvfacets_ro "")
(define ref_uvfacets_ps "")
(define uvfacets-fill #f)

(define face-uvfacets (lambda ()
	(let	(
				(picked_face (part:selection))
			)
	(if (>(length picked_face) 0)
	(begin
		(define picked_face (car picked_face))
		(if (face? picked_face)
		(begin
			(if (not (view? uvfacets_view))
				(begin
					(set! uvfacets_view (view:gl 500 0 500 500))
					(view:detach uvfacets_view)
				)
			)
			(if (ro? uvfacets_ro)
				(ro:erase uvfacets_ro uvfacets_view)				
			)
			(set! uvfacets_ro (ro:face-uvfacets picked_face uvfacets-fill))
			(ro:erase uvfacets_ro)
			(ro:display uvfacets_ro uvfacets_view)
			(top uvfacets_view)
			(zoom-all uvfacets_view)
			)
			(display "FACE selection expected.\n")
		)
	)
	(display "Nothing picked.\n")
	)
	)
	)
)

(define uvfacets_ps
	(lambda (n)
		(cond 
			((equal? n "EndPick") 
				(face-uvfacets)
			)
		)
	)
)

(define show-uvfacets
	(lambda (on-off fill)
	(set! uvfacets-fill fill)
	(if on-off
		(begin
			(display "Turning on uvfacets display.  Pick a face to show the uvfacets.\n")
			(set! ref_uvfacets_ps (part:add-picksubscriber-script "uvfacets_ps"))
		)
		(begin
			(display "Turning off uvfacets display.\n")
			(part:remove-picksubscriber ref_uvfacets_ps)
		)
	)
))
